package com.ocm.tourism.picasaweb.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.http.protocol.HTTP;
import org.apache.http.util.ByteArrayBuffer;

public class Multipart {

   /**
    * Line ending
    */
   private static final String CR_LF = "\r\n";

   /**
    * Dashes for the boundary
    */
   private static final String DASHES = "--";

   /**
    * Buffer that holds the content
    */
   private ByteArrayBuffer buffer = new ByteArrayBuffer(0);

   private String boundary;

   public Multipart() {
      
   }

   public Multipart(String description, String boundary) throws UnsupportedEncodingException
   {
     // set boundary
     this.boundary = CR_LF+DASHES+boundary;

     // merge description
     mergePart(description);
   }

   @SuppressWarnings("deprecation")
   public byte[] getContent() throws UnsupportedEncodingException
   {
     // create new byte array buffer and get start and end as byte arrays
     ByteArrayBuffer endBuffer = new ByteArrayBuffer(0);
     byte[] start              = buffer.toByteArray();
     byte[] end                = (boundary+DASHES).getBytes(HTTP.DEFAULT_CONTENT_CHARSET);

     // append the start and end to new byte array buffer
     endBuffer.append(start, 0, start.length);
     endBuffer.append(end, 0, end.length);

     // return buffer as content
     return endBuffer.toByteArray();
   }
   
   public String getBoundary(){
      return this.boundary;
   }

   public void addPart(String content, String contentType) throws UnsupportedEncodingException
   {
     mergePart(createStart(contentType) + content);
   }

   public void addPart(String content) throws UnsupportedEncodingException
   {
     mergePart(content);
   }
   
   public void addPart(File file, String contentType) throws UnsupportedEncodingException, FileNotFoundException, IOException
   {
     // merge start into content
     mergePart(createStart(contentType));

     // create vars to reas file
     InputStream is = new FileInputStream(file);
     byte[] bytes   = new byte[(int)file.length()];
     int offset     = 0;
     int numRead    = 0;

     // reaf file into byte array
     while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length - offset)) >= 0) {
       offset += numRead;
     }

     // errror checking
     if (offset < bytes.length) {
       throw new IOException("Could not completely read file "+file.getName());
     }

     // close input stream
     is.close();

     // merge the file into content
     mergePart(bytes);
   }

   public void addPart(byte[] bytes, String contentType) throws UnsupportedEncodingException, FileNotFoundException, IOException
   {
     // merge start into content
     mergePart(createStart(contentType));
     mergePart(bytes);
   }
   
   private String createStart(String contentType)
   {
     return boundary+CR_LF+"Content-Type: "+contentType+CR_LF+CR_LF;
   }

   @SuppressWarnings("deprecation")
   private void mergePart(Object input) throws UnsupportedEncodingException
   {
     byte[] bytes = null;

     if (input instanceof String) {
       bytes = ((String)input).getBytes(HTTP.DEFAULT_CONTENT_CHARSET);
     } else if (input instanceof byte[]) {
       bytes = (byte[])input;
     }

     if (bytes != null) {
       buffer.append(bytes, 0, bytes.length);
     }
   }
}
